package com.traffic.between.adaptor;

import java.util.Date;
import org.apache.log4j.Logger;

import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.FuYanServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;

@ChooseProvider(channel = "fy")
public class FuYanAdaptor implements IAdaptor{

	private static Logger log = Logger.getLogger(FuYanAdaptor.class);

	
	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		
		String product = orderBean.getProductCode();
		String phone = orderBean.getPhoneNo();
		String orderId = orderBean.getIds();
	
		String result = FuYanServiceImpl.service.order(product, phone, orderId);
		
		JSONObject json = JSONObject.fromObject(result);

		String code = json.getString("code");
		if ("000000".equals(code)) {
			orderBean.setErrorMsg(json.getString("msg"));
			orderBean.setStatus("11");
			orderBean.setMessage(json.getString("msg"));
		} else {
			orderBean.setErrorMsg(json.getString("msg"));
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("msg"));
		}

		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	
	
	public void status() {
		OrderBean orderBean = OrderInfoContext.getContext();
		
		String cstmOrderNo = orderBean.getIds(); 
		log.info("between -福研状态FuYanService.orderStatus() 查询请求param<>cstmOrderNo :" + cstmOrderNo);

		String result = FuYanServiceImpl.service.orderStatus(cstmOrderNo);
		log.info("between- 福研状态FuYanService.orderStatus() 查询响应response<> :" + result);
		

		JSONObject json = JSONObject.fromObject(result);

		String code = json.getString("code");

		if ("111111".equals(code)) {

			orderBean.setErrorMsg("订购成功");
			orderBean.setStatus("7");
			orderBean.setMessage(json.getString("msg"));
		} else if ("000000".equals(code) || "444444".equals(code)
				|| "888888".equals(code)) {

			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("1");
			orderBean.setMessage(json.getString("msg"));
		} else {
			orderBean.setErrorMsg(json.getString("msg"));
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("msg"));
		}
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
